package sample;

import common.Constant;
import lombok.Data;
import bean.FetchParams;
import annotation.FieldXpath;
import spider.Spider;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by chen_ on 2017/5/6.
 */
public class CsdnSpider {
    public static Spider spider = new Spider();

    @Data
    public static class CsdnBlog {
        @FieldXpath("*[@class=link_title]/a/text()")
        private String title;// 标题
        @FieldXpath("*[@class=link_categories]/a/text()")
        private String category;// 分类
        @FieldXpath("*[@class=link_view]/text()")
        private String view;// 阅读人数
        @FieldXpath("*[@class=link_comments]/text()")
        private String comments;// 评论人数
    }

    private static void init() {
        LinkedBlockingQueue<String> q = new LinkedBlockingQueue<>(Constant.CAPACITY);
        for (int i = 1; i < 9; i++) {
            q.offer("http://blog.csdn.net/yueqian_scut/article/list/" + i);
        }
        spider.setQueues0(q);
    }

    public static void main(String[] args) {
        init();
        spider.chain(url ->
                FetchParams.builder().url((String) url).flag(FetchParams.Flag.dynamic).xpath("*[@class=link_title]/a/@href").build()
        ).chain(url -> FetchParams.builder().url("http://blog.csdn.net" + url).flag(FetchParams.Flag.dynamic).clazz(CsdnBlog.class).build()).print().end();
    }
}